/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.buzz.student.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.buzz.student.entity.Student;
import com.jeeplus.modules.buzz.student.mapper.StudentMapper;
import com.jeeplus.modules.buzz.student.entity.Studententry;
import com.jeeplus.modules.buzz.student.mapper.StudententryMapper;

/**
 * 学生登记	Service
 * @author 陈
 * @version 2025-06-23
 */
@Service
@Transactional(readOnly = true)
public class StudentService extends CrudService<StudentMapper, Student> {

	@Autowired
	private StudententryMapper studententryMapper;
	
	public Student get(String id) {
		Student student = super.get(id);
		student.setStudententryList(studententryMapper.findList(new Studententry(student)));
		return student;
	}
	
	public List<Student> findList(Student student) {
		return super.findList(student);
	}
	
	public Page<Student> findPage(Page<Student> page, Student student) {
		return super.findPage(page, student);
	}
	
	@Transactional(readOnly = false)
	public void save(Student student) {
		super.save(student);
		for (Studententry studententry : student.getStudententryList()){
			if (studententry.getId() == null){
				continue;
			}
			if (Studententry.DEL_FLAG_NORMAL.equals(studententry.getDelFlag())){
				if (StringUtils.isBlank(studententry.getId())){
					studententry.setInterid(student);
					studententry.preInsert();
					studententryMapper.insert(studententry);
				}else{
					studententry.preUpdate();
					studententryMapper.update(studententry);
				}
			}else{
				studententryMapper.delete(studententry);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(Student student) {
		super.delete(student);
		studententryMapper.delete(new Studententry(student));
	}
	
}